package com.youlu.myapplication.scratch;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import java.util.List;

/**
 * @author sym
 */
public class ScratchView extends View {

    public ScratchCallBack mScratchCallBack;

    public ScratchView(Context context) {
        super(context);
    }

    public ScratchView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public ScratchView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    @Override
    public void onDraw(Canvas canvas) {
        if (null != mScratchCallBack) {
            List<Path> paths = mScratchCallBack.getPaths();
            List<Boolean> booleans = mScratchCallBack.onDraw();
            if (paths.size() != 0) {
                int currentPathSize = mScratchCallBack.getCurrentPathSize();
                while (currentPathSize > 0
                        && paths.get(currentPathSize - 1) != ScratchFragment.mEmptyPath) {
                    currentPathSize--;
                }
                while (currentPathSize < Math.min(mScratchCallBack.getCurrentPathSize(), paths.size())) {
                    if (currentPathSize >= booleans.size() || booleans.get(currentPathSize)) {
                        canvas.drawPath(paths.get(currentPathSize), mScratchCallBack.getPaint());
                    }
                    currentPathSize++;
                }
            }
            if (mScratchCallBack.path() != null) {
                canvas.drawPath(mScratchCallBack.path(), this.mScratchCallBack.getPaint());
            }
        }
    }

    public void setScratchCallBack(ScratchCallBack callBack) {
        this.mScratchCallBack = callBack;
    }

    public interface ScratchCallBack {
        /**
         * 获取画布用的画笔
         *
         * @return 画笔
         */
        Paint getPaint();

        /**
         * 画笔上的所有已画路径的集合
         *
         * @return 路径
         */
        List<Path> getPaths();

        /**
         * 返回当前路径
         *
         * @return 路径
         */
        Path path();

        int getCurrentPathSize();

        /**
         * 找出没有被绘画的path
         *
         * @return path 集合
         */
        List<Boolean> onDraw();
    }
}